/*
 * 软件著作权：北京康兰尚德数据科技有限公司
 * 项目名称：康兰数据购物中心分析平台
 *
 * NAME : UserDaoImpl.java
 *
 * PURPOSE : 网店数据库访问
 *
 * AUTHOR : EEDATA/ywb
 *
 *
 * 创建日期: 2013-12-06
 * HISTORY：
 * 变更日期 
 */
package dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import pojo.EcpYhUser;
import util.HibernateUtils;

/**
 * 用户数据库访问层
 * 
 * @author ywb
 */

public class UserDaoImpl {

    /**
     * 用户注册
     */
    public boolean insertUser(EcpYhUser user) {// 十六、registerNewUser接口
        Session session = null;
        Transaction ts = null;
        boolean row = true;
        try {
            session = HibernateUtils.getSession();
            ts = session.beginTransaction();

            session.persist(user);

            ts.commit();

        } catch (Exception e) {
            e.printStackTrace();
            row = false;
            ts.rollback();
        } finally {
            HibernateUtils.closeSession(session);
        }

        return row;

    }


    /**
     * 用户登录
     */
    @SuppressWarnings("unchecked")
    public String login(String email, String pwd) {// 十七、login接口
        Session session = null;
        Transaction ts = null;
        String result = "-1";
        try {
            session = HibernateUtils.getSession();
            ts = session.beginTransaction();

            Query query = session.createQuery("from EcpYhUser where email=? and userpwd=?");
            query.setParameter(0, email);
            query.setParameter(1, pwd);
            List<EcpYhUser> list = query.list();

            if (list.size() > 0)
                result = "" + list.get(0).getUserid();

            ts.commit();

        } catch (Exception e) {
            e.printStackTrace();
            ts.rollback();
        } finally {
            HibernateUtils.closeSession(session);
        }

        return result;

    }


    /**
     * 退出登录
     */
    @SuppressWarnings("unchecked")
    public boolean exitlogin(String userid, String pwd) {// 十八、logout接口
        Session session = null;
        Transaction ts = null;
        boolean result = false;
        try {
            session = HibernateUtils.getSession();
            ts = session.beginTransaction();

            Query query = session.createQuery("from EcpYhUser where userid=? and userpwd=?");
            query.setParameter(0, Integer.parseInt(userid));
            query.setParameter(1, pwd);
            List<EcpYhUser> list = query.list();

            if (list.size() > 0)
                result = true;

            ts.commit();

        } catch (Exception e) {
            e.printStackTrace();
            ts.rollback();
        } finally {
            HibernateUtils.closeSession(session);
        }

        return result;

    }
}
